package com.asd.map.controller;

import com.alibaba.fastjson.JSONArray;
import com.asd.common.Result;
import com.asd.map.entity.Line_style;
import com.asd.map.entity.vo.Line_styleVO;
import com.asd.map.service.ILine_styleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @createtime 2021-10-15 9:44
 * @description
 */
@RestController
@RequestMapping("/api/line_style")
@Api(tags = "线样式Api")
public class Line_styleController {

    @Autowired
    private ILine_styleService line_styleService;


    /**
     * 编辑
     *
     * @param
     * @return
     */
    @ApiOperation(value = "线样式-编辑", notes = "线样式-编辑")
    @PutMapping(value = "/update")
    public Result<?> edit(@RequestBody Line_style line_style) {

        List<Line_style> styleList = line_styleService.findByApplyId(line_style.getApplyId(), line_style.getDid());


        if (styleList.size() == 0) {//不存在线样式时就新增

            line_styleService.save(line_style);

            return Result.ok(line_style);
        } else {

//            for (Line_style line_style1:styleList) {
                for (int i = 0; i < styleList.size(); i++) {

                    Line_style line_style1 = styleList.get(i);

                    if (line_style1.getType().equals("0")){//存在样式为统一样式时

                    Line_style oldLine_style = line_styleService.getById(line_style1.getId());

                    oldLine_style.setType(line_style.getType());

                    if (!(line_style.getStyle().equals(null) || line_style.getStyle().equals(""))) {

                        oldLine_style.setStyle(line_style.getStyle());
                    }
                    if (!(line_style.getSize().equals(null) || line_style.getSize().equals(""))) {
                        oldLine_style.setSize(line_style.getSize());
                    }
                    if (!(line_style.getColor().equals(null) || line_style.getColor().equals(""))) {
                        oldLine_style.setColor(line_style.getColor());
                    }

                    line_styleService.updateById(oldLine_style);
                }else {//存在样式不为统一样式时
                    line_styleService.removeById(line_style1.getId());
                        if (i == styleList.size() - 1) {
                            line_styleService.save(line_style);
                        }
                }
            }

            return Result.ok(line_style);

        }
    }


    /**
     * 分类样式
     *
     * @param
     * @return
     */
    @ApiOperation(value = "线样式-分类样式", notes = "线样式-分类样式")
    @PutMapping(value = "/updateStyleTriage")
    public Result<?> updateStyleTriage(@RequestBody Line_styleVO linestyleVO) {


        List<Line_style> styleList = line_styleService.findByApplyId(linestyleVO.getApplyId(), linestyleVO.getDid());
        List<Line_style> list = new ArrayList<>();

        if (styleList.size() > 0) {
            for (Line_style line_style:styleList) {
                line_styleService.removeById(line_style.getId());
            }
        }

        String data = linestyleVO.getData();

        List<Map<String, String>> dataList = (List<Map<String, String>>) JSONArray.parse(data);
        for (int i = 0; i < dataList.size(); i++) {
            Map<String, String> map = dataList.get(i);
            String lid = map.get("lid");
            String size = map.get("size");
            String color = map.get("color");
            String style = map.get("style");

            Line_style lineStyle = new Line_style();
            lineStyle.setApplyId(linestyleVO.getApplyId());
            lineStyle.setDid(linestyleVO.getDid());
            lineStyle.setLid(lid);
            lineStyle.setType(linestyleVO.getType());
            lineStyle.setSize(size);
            lineStyle.setColor(color);
            lineStyle.setStyle(style);
            line_styleService.save(lineStyle);
            list.add(lineStyle);
        }

        return Result.ok(list);

    }


    /**
     * 分类样式-单条线样式修改
     *
     * @param
     * @return
     */
    @ApiOperation(value = "点位样式-分类样式-根据id修改单条线样式修改", notes = "点位样式-分类样式-根据id修改单条线样式修改")
    @PutMapping(value = "/updateStyleById")
    public Result<?> updateStyleById(@ApiParam(name = "id",value = "样式ID", required = true) @RequestParam("id") String id,
                                     @ApiParam(name = "size",value = "线的粗细")@RequestParam("size")String size,
                                     @ApiParam(name = "style",value = "实线：0;虚线：1")@RequestParam("style")String style,
                                     @ApiParam(name = "color",value = "颜色")@RequestParam("color") String color) {

        Line_style lineStyle = line_styleService.getById(id);
        if (lineStyle==null){
            return Result.error("修改的样式不存在！");
        }else {
            lineStyle.setSize(size);
            lineStyle.setStyle(style);
            lineStyle.setColor(color);
            line_styleService.updateById(lineStyle);
            return Result.ok(lineStyle);
        }
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "线样式-通过id删除", notes = "线样式-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {

        line_styleService.removeById(id);


        return Result.ok("删除成功!");
    }

    /**
     * 通过id删除
     *
     * @param
     * @return
     */
    @ApiOperation(value = "线样式-删除数据源关联样式", notes = "线样式-通过id删除")
    @DeleteMapping(value = "/deleteForInformation")
    public Result<?> delete1(@RequestParam(name = "applyId", required = true) String applyId, @RequestParam(name = "did", required = true) String did) {

        line_styleService.deleteById(applyId, did);


        return Result.ok("删除成功!");
    }


}
